﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using RpgLibrary;

namespace RpgEditor
{
    public partial class FormDetailBase : Form, IRpgDataObject
    {
        private IRpgDataObject dataObject = null;
        public virtual IRpgDataObject DataObject
        {
            get { return dataObject; }
            set { dataObject = value; }
        }

        protected delegate bool SetDataObjectValueDelegate();
        protected delegate void LoadDataObjectValueDelegate();

        protected SetDataObjectValueDelegate SetDataObjectValueFunction = null;
        protected LoadDataObjectValueDelegate LoadDataObjectValueFunction = null;

        public FormDetailBase()
        {
            InitializeComponent();

            this.Load += new EventHandler(FormDetails_Load);
            this.FormClosing += new FormClosingEventHandler(FormDetails_FormClosing);
        }

        protected void FormDetails_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
            }
        }

        void FormDetails_Load(object sender, EventArgs e)
        {
            if (LoadDataObjectValueFunction != null)
                LoadDataObjectValueFunction();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (SetDataObjectValueFunction != null && SetDataObjectValueFunction())
            {
                this.FormClosing -= FormDetails_FormClosing;
                this.Close();
            }
            else
                throw new Exception("SetDataObjectValueFunction is not set for this form.");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            DataObject = null;
            this.FormClosing -= FormDetails_FormClosing;
            this.Close();
        }
    }
}
